package com.platform.modules.mch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.ValidUtils;
import com.platform.common.utils.mch.HttpUtils;
import com.platform.common.utils.mch.config.MchConfig;
import com.platform.modules.mall.entity.MallOrderEntity;
import com.platform.modules.mall.entity.MallOrderGoodsEntity;
import com.platform.modules.mall.service.MallOrderGoodsService;
import com.platform.modules.mch.dto.OrderEntity;
import com.platform.modules.mch.service.MchOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MchOrderServiceImpl implements MchOrderService {

    @Resource
    private MallOrderGoodsService orderGoodsService;

    @Override
    public JSONObject orders(MallOrderEntity order, boolean check) {
        // 用户名和手机号不能为空
        ValidUtils.allNotNull(order.getConsignee(),order.getMobile());
        List<MallOrderGoodsEntity> orderGoodsList = orderGoodsService.list(new LambdaQueryWrapper<MallOrderGoodsEntity>()
                .eq(MallOrderGoodsEntity::getOrderId, order.getId()));
        if (orderGoodsList.size() != 1) {
            throw new BusinessException("不支持的操作！");
        }
        MallOrderGoodsEntity orderGoods = orderGoodsList.get(0);
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(order.getId());
        orderEntity.setActivityId(order.getActivityId());
        orderEntity.setFromType(order.getFromType());
        orderEntity.setGoodsId(orderGoods.getGoodsId());
        orderEntity.setPhone(order.getMobile());
        orderEntity.setPostscript(order.getPostscript());
        orderEntity.setShopsId(order.getShopsId());
        orderEntity.setNumber(orderGoods.getNumber());
        orderEntity.setName(order.getConsignee());
        orderEntity.setSystemCode(MchConfig.getSystemCode());
        // 站点ID （联联订单必填）
        orderEntity.setLocationId(MchConfig.getLocationId());
        orderEntity.setVerify(check);
        return HttpUtils.sendPost(MchConfig.Url.order.getUrl(), JSONObject.toJSONString(orderEntity));
    }

}
